
#include <iostream>
#include <sstream>
#include <string> 
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <array>
using namespace std;


struct Node{
    std::array<bool,1000> bitset{};
    int count =0 ;
    bool empty = true;
    void set(int num){
        if(!bitset[num]){
            bitset[num] = true;
            ++count;
        }
        empty = false;
    }
    void unset(int num){
        if(bitset[num]){
            bitset[num] = false;
            --count;
        }
        if(count==0){
            empty = true;
        }
    }
};

int main(){
    int n;
    cin>>n;
    vector<Node> nodes(n);
    for(int i=0; i<n;i++){
        int p_num;
        cin>>p_num;
        for(int j =0; j<p_num; j++){
            int pre;
            cin>>pre;
            nodes[i].set(pre-1);
        }
    }
    int result = 0;
    set<int> overs{};
    while(true){
        vector<int> runs;
        for(int i =0; i< n; i++){
            if(nodes[i].empty && overs.find(i)==overs.end()){
                runs.push_back(i);
            }
        }
        if(runs.empty()){
            cout<<"-1"<<endl;
            return 0;
        }
        ++result;
        for(auto const& run : runs){
            overs.insert(run);
            for(auto &node : nodes){
                node.unset(run);
            }
        }
        if(overs.size()==n){
            cout<<result<<endl;
            return 0;
        }
    }

    return -1;
}